<?php
namespace app\admin\controller;

use think\Controller;
use think\Log;
use think\Exception;

class User extends Base
{

    /**
     * 获取用户列表
     * @author wcy
     * @version 2017年4月19日09:39:46
     */
    public function userList(){
        try{
            if($this->request->isAjax()||$this->request->isPost()){
                $data = $this->deDataTablesJson();
                $draw=$data['draw'];
                $start=$data["start"];
                $length=$data["length"];
                $search="";
                if(!empty($_POST["search"])){
                    $search=$_POST["search"];
                }
                $data=model("User")->userList($search,$start,$length);
                if(!$data){
                    return json(["draw"=>$draw,"recordsTotal"=>0,"recordsFiltered"=>0,"data"=>[]]);
                }else{
                    return json(["draw"=>$draw,"recordsTotal"=>$data["count"],"recordsFiltered"=>$data["count"],"data"=>$data["data"]]);
                }
            }else{
                return view("userlist");
            }
        }catch (Exception $e){
            Log::record($e->getMessage());
            return false;
        }
    }
    /**
     * 添加用户
     * @author wcy
     * @version 2017年4月19日11:18:02
     */
    public function addUser(){
        try{
            if($this->request->isPost()||$this->request->isAjax()){
                $data["group_id"]=$_POST["group_id"];
                $data["name"]=$_POST["name"];
                $data["username"]=$_POST["username"];
                $password=$_POST["password"];
                $data["password"]=md5($password);
                $id="";
                $count=model("User")->getUserByName($id,$data["username"]);
                if($count>0){
                    return json(["code"=>-1,"msg"=>"账号存在,请重新输入!"]);
                }
                $res=model("User")->addUser($data);
                if($res!==false){
                    return json(["code"=>1,"msg"=>"添加成功"]);
                }else{
                    return json(["code"=>-1,"msg"=>"添加失败"]);
                }
            }else{
                $data=model("AuthGroup")->getGroupAll();
                $this->assign("group_data",$data);
                return view("adduser");
            }
        }catch (Exception $e){
            Log::record($e->getMessage());
            return view("adduser");
        }
    }
    /**
     * 编辑用户
     * @author wcy
     * @version 2017年4月19日11:31:17
     */
    public function editUser(){
        try{
            if($this->request->isAjax()||$this->request->isPost()){
                $id=$_POST["id"];
                $data["group_id"]=$_POST["group_id"];
                $data["name"]=$_POST["name"];
                $data["username"]=$_POST["username"];
                if(!empty($_POST["password"])){
                    $data["password"]=md5($_POST["password"]);
                }
                $count=model("User")->getUserByName($id,$data["username"]);
                if($count>0){
                    return json(["code"=>-1,"msg"=>"账号存在,请重新输入!"]);
                }
                $res=model("User")->editUser($id,$data);
                if($res!==false){
                    return json(["code"=>1,"msg"=>"修改成功"]);
                }else{
                    return json(["code"=>-1,"msg"=>"修改失败"]);
                }
            }else{
                $id=$_GET["id"];
                $data=model("User")->getUserById($id);
                $group_data=model("AuthGroup")->getGroupAll();
                $this->assign("group_data",$group_data);
                $this->assign("user_data",$data);
                return view("edituser");
            }
        }catch (Exception $e){
            Log::record($e->getMessage());
            return view("edituser");
        }
    }
    /**
     * 删除用户
     * @author wcy
     * @version 2017年4月19日09:39:46
     */
    public function delUser(){
        try{
            $id=$_POST["id"];
            $res=model("User")->delUser($id);
            if($res!==false){
                return json(["code"=>1,"msg"=>"删除成功"]);
            }else{
                return json(["code"=>-1,"msg"=>"删除失败"]);
            }
        }catch (Exception $e){
            Log::record($e->getMessage());
            return json(["code"=>-2,"msg"=>"系统出错,请刷新页面"]);
        }
    }
}